# pyzbar用于解析各种类型的条形码和二维码
# decode函数用于从图像中识别和解码条形码或二维码
from pyzbar.pyzbar import decode
from PIL import Image
import requests

# 从二维码中解析出APK的URL
def get_apk_url_from_qrcode(qrcode_image_path):
    decoded_objects = decode(Image.open(qrcode_image_path))
    for obj in decoded_objects:
        if obj.type == 'URI':
            return obj.data.decode('utf-8')
    return None

# qrcode_image_path = "D:\\APP_Detection\\QRcodeanylor\\qrcode_img\\qrcode_image1.jpg"
# apk_url = get_apk_url_from_qrcode(qrcode_image_path)
# print(apk_url)

# 下载APK文件
def download_apk(url, save_path):
    response = requests.get(url, stream=True)  # stream=True允许以流的形式接受响应
    if response.status_code ==200:  # 检查HTTP响应的状态码是否为200，即请求成功
        with open(save_path, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):  # 以chunk_size为单位迭代响应体中的数据块
                if chunk:
                    f.write(chunk)
                else:
                    print("Failed to download APK")

apk_url = "https://android-apps.pp.cn/fs08/"
apk_url = "https://android-apps.pp.cn/fs08/2024/07/11/5/110_541a643a0dd4e110774245b743afc731.apk"
apk_save_path = "C:/file_data/涉诈APP/test_online_apk/" + apk_url.split("/")[-1] + '.apk'
download_apk(apk_url, apk_save_path)
